#!/bin/bash

patch="${1:-}"

name="controlrd"

# Get the current date in the specified format
version=$(date '+%Y.%m.%d')

# build the plugin

# make local
go build fmt
env CGO_ENABLED=0 go build -ldflags "-X main.Version=$version$patch" -gcflags "all=-trimpath=$GOPATH" -v -o meta/plugin/$name

echo "$version$patch" >meta/plugin/VERSION

cd meta
mv plugin $name
tar --owner=root --group=root -czvf ../$name-$version$patch.tgz $name
cd ..

# Calculate the MD5 checksum for the plugin
md5=$(md5sum $name-$version$patch.tgz | awk '{print $1}')

# Get the latest git tag
latest_tag=$(git describe --tags --abbrev=0)

# Generate the changelog since the latest tag
changes=$(git log --oneline $latest_tag..HEAD --pretty="- %s")

# Use awk for replacements and generate plugin file
awk -v version="$version$patch" -v md5="$md5" -v changes="$changes" '
  {
    gsub(/##version##/, version);
    gsub(/##md5##/, md5);
    gsub(/##changes##/, changes);
    print $0;
  }
' meta/template/$name.plg >$name.plg

# tag the release
git tag $version$patch
git push origin $version$patch

# release the plugin
# Create a release with the tag name
gh release create $version$patch --notes "$changes" --repo "github.com/jbrodriguez/$name"

# Upload the TGZ artifact
gh release upload $version$patch ./$name-$version$patch.tgz --clobber --repo "github.com/jbrodriguez/$name"

# Upload the XML artifact
gh release upload $version$patch ./$name.plg --clobber --repo "github.com/jbrodriguez/$name"
